package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.scm.entity.ScmCusInfo;
import org.jeecg.modules.scm.entity.ScmReceivablesTransferApplication;
import org.jeecg.modules.scm.service.IScmCusInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
/**
 * @Description: scm_cus_info
 * @Author: jeecg-boot
 * @Date:   2021-10-22
 * @Version: V1.0
 */
@Api(tags="客户信息管理")
@RestController
@RequestMapping("/cus_info/scmCusInfo")
@Slf4j
public class ScmCusInfoController extends JeecgController<ScmCusInfo, IScmCusInfoService> {

	 @Resource
	 private IScmCusInfoService scmCusInfoService;


	/**
	 * 分页列表查询
	 *
	 * @param scmCusInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "分页列表查询")
	@ApiOperation(value="分页列表查询", notes="分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ScmCusInfo scmCusInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   @RequestParam(name="isLoginUser",required = false) boolean isLoginUser,
								   HttpServletRequest req) {
		if(isLoginUser){
			LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			scmCusInfo.setCusManage(user.getUsername());
		}
		QueryWrapper<ScmCusInfo> queryWrapper = QueryGenerator.initQueryWrapper(scmCusInfo, req.getParameterMap());
		Page<ScmCusInfo> page = new Page<ScmCusInfo>(pageNo, pageSize);
		IPage<ScmCusInfo> pageList = scmCusInfoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 通过id查询
	 *
	 * @param cusNo
	 * @return
	 */
	@AutoLog(value = "通过id查询")
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ScmCusInfo> queryById(@RequestParam(name = "cusNo", required = true) String cusNo) {
		ScmCusInfo scmCusInfo = scmCusInfoService.getById(cusNo);
		if (scmCusInfo == null) {
			return Result.error("未找到对应数据", null);
		}
		return Result.OK(scmCusInfo);
	}

	/**
	 *   添加
	 *
	 * @param scmCusInfo
	 * @return
	 */
	@AutoLog(value = "添加")
	@ApiOperation(value="添加", notes="添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ScmCusInfo scmCusInfo) {
		if(scmCusInfo.getCusName()!=null && scmCusInfoService.getNoByName(scmCusInfo.getCusName())!=null){
			return Result.error("该客户已存在");
		}
		scmCusInfo = scmCusInfoService.ctreateEnterprise(scmCusInfo);
		//默认关闭状态
		scmCusInfo.setCusState("80");
		scmCusInfoService.save(scmCusInfo);
		return Result.OK(scmCusInfo.getCusNo());
	}

	/**
	 *  编辑
	 *
	 * @param scmCusInfo
	 * @return
	 */
	@AutoLog(value = "编辑")
	@ApiOperation(value="编辑", notes="编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ScmCusInfo scmCusInfo) {
		scmCusInfo = scmCusInfoService.ctreateEnterprise(scmCusInfo);
		scmCusInfoService.updateById(scmCusInfo);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过cus_no删除
	 *
	 * @param cus_no
	 * @return
	 */
	@AutoLog(value = "通过cus_no删除")
	@ApiOperation(value="通过cus_no删除", notes="通过cus_no删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="cus_no",required=true) String cus_no) {
		scmCusInfoService.removeById(cus_no);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "批量删除")
	@ApiOperation(value="批量删除", notes="批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.scmCusInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过指定字段查询
	 *
	 * @return
	 */
	@AutoLog(value = "通过指定字段查询")
	@ApiOperation(value="通过指定字段查询", notes="通过指定字段查询")
	@GetMapping(value = "/queryByField")
	public Result<?> queryByField(@RequestParam(name="type",required=true) String type,
							   @RequestParam(name="value",required=true) String value) {
		QueryWrapper<ScmCusInfo> queryWrapper = new QueryWrapper<ScmCusInfo>();
		queryWrapper.eq(type,value);
		List<ScmCusInfo> list = scmCusInfoService.list(queryWrapper);
		if(list==null||list.isEmpty()) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(list);
	}

	 /**
	  * 通过多字段查询
	  *
	  * @return
	  */
	 @AutoLog(value = "通过多字段查询")
	 @ApiOperation(value="通过多字段查询", notes="通过多字段查询")
	 @PutMapping(value = "/queryByFields")
	 public Result<?> queryByFields(@RequestBody Map<String,Object> scmCusInfo) {
		 List<ScmCusInfo> list = scmCusInfoService.queryByFields(scmCusInfo);

		 if(list==null||list.isEmpty()) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(list);
	 }

	 /**
	  * 	模糊查询客户
	  *
	  * */
	 @AutoLog(value = "模糊查询")
	 @ApiOperation(value="模糊查询", notes="模糊查询")
	 @GetMapping(value = "/fuzzyQuery")
	 public Result<?> fuzzyQuery(
	 		@RequestParam(name="type",required=true) String type,
	 		@RequestParam(name="value",required=true) String value,
			@RequestParam(name="fields", required=false) String fields
			)
	 {

		List<ScmCusInfo> result = scmCusInfoService.fuzzyQuery(type,value,fields);
		if(result==null||result.isEmpty()) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(result);
	 }

	 /**
	 * 查询客户服务人员
	 *
	 * */
//	 @AutoLog(value = "scm_cus_info-查询客户服务人员")
//	 @ApiOperation(value="scm_cus_info-查询客户服务人员", notes="scm_cus_info-查询客户服务人员")
//	 @GetMapping(value = "/findCusServer")
//	 public Result<?> findCusServer(@RequestParam(name="cusNo",required=true) String cusNo){
//
//		 List<ScmCusServer> serverName = scmCusInfoService.findServerName(cusNo);
//		 System.out.println(serverName);
//		 if(serverName!=null) {
//		 	return Result.OK(serverName);
//		 }
//
//		 return Result.error("查无相关服务人员");
//	 }
	/**
	 * 通过客户号获取客户名称
	 *
	 * @return
	 */
	@AutoLog(value = "通过客户号获取客户名称")
	@ApiOperation(value="通过客户号获取客户名称", notes="通过客户号获取客户名称")
	@GetMapping(value = "/getNameByNo")
	public Result<?> getNameByNo(@RequestParam(name="cus_no",required=true) String cusNo) {
		ScmCusInfo obj = scmCusInfoService.getById(cusNo);
		if(obj==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(obj.getCusName());
	}

	/**
	 * 通过客户名称获取客户号
	 *
	 * @return
	 */
	@AutoLog(value = "通过客户名称获取客户号")
	@ApiOperation(value="通过客户名称获取客户号", notes="通过客户名称获取客户号")
	@GetMapping(value = "/getNoByName")
	public Result<?> getNoByName(@RequestParam(name="cus_name",required=true) String cusName) {
		Integer cusNo = scmCusInfoService.getNoByName(cusName);
		if(cusNo==null){
			return Result.error("查询不到数据");
		}
		return Result.OK(cusNo);
	}
	/**
	 *
	 * 上传附件
	 * */

	@ApiOperation(value = "上传附件", notes = "上传附件")
	@PostMapping("/uploadFile")
	public Result<?> uploadFile(@RequestBody MultipartFile[] files,@RequestParam(name="cusNo",required = false) Integer cusNo,HttpServletRequest req) {
		System.out.println("req:"+req);
		Enumeration<String> names = req.getHeaderNames();
		while(names.hasMoreElements()){
			String head = names.nextElement();
			System.out.println("head:"+head+req.getHeader(head));
		}
		System.out.println(req.getParameter("cusNo"));
		String res = scmCusInfoService.uploadAccessory(files, cusNo);
		return Result.OK(res);
	}


    /**
    * 导出excel
    *
    * @param request
    * @param scmCusInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmCusInfo scmCusInfo) {
        return super.exportXls(request, scmCusInfo, ScmCusInfo.class, "scm_cus_info");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmCusInfo.class);
    }



}
